package com.first.controller;

import com.first.dao.OOrderDao;
import com.first.entity.*;
import com.first.util.LayTab;
import com.first.util.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("oorder")
public class OOrderController {
    @Resource
    private OOrderDao oOrderDao;

    /**
     * 通过出库单修改book数量
     * @param oOrder
     * @param request
     * @return
     */
    @RequestMapping("/updateBookCount")
    public Result updateBookCount(OOrder oOrder,HttpServletRequest request){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        Result result = new Result();
        User user = (User) request.getSession().getAttribute("user");
        String w_name = user.getUsername();
        oOrder.setW_name(w_name);
        oOrder.setO_orderoutdate(dateString);
        String s = oOrderDao.updateBookCount(oOrder);

            if("".equals(s)){
                result.setMsg("出库成功");
                result.setCode(1);
            }else {
                result.setCode(2);
                result.setMsg(s+"库存不足");
            }
        return result;
    }

    /**
     * 将订单驳回
     * @param oOrder
     * @param request
     * @return
     */
    @RequestMapping("/bh")
    public Result bhOOrder(OOrder oOrder,HttpServletRequest request){
        Result result = new Result();
        User user = (User) request.getSession().getAttribute("user");
        String w_name = user.getUsername();
        oOrder.setW_name(w_name);
        int i = oOrderDao.bhOOrder(oOrder);
        if(i>0){
            result.setMsg("驳回成功");
            result.setCode(1);
        }else {
            result.setCode(2);
            result.setMsg("驳回失败");
        }
        return result;
    }

    @RequestMapping("/oorderstatus2")
    public LayTab oorderStatus2(OOrder oOrder){
        List<Map> list = oOrderDao.oorderStatus2(oOrder);
        LayTab layTab = new LayTab();
        if (list !=null){
            layTab.setCount(oOrderDao.oorderStatus2Count(oOrder));
            layTab.setData(list);
        }
        return layTab;
    }

    /**
     * 查询所有出库单信息
     * @param oOrder
     * @return
     */
    @RequestMapping("/queryAllOutOrder")
    public LayTab  queryAllOutOrder(OOrder oOrder){
        List<Map> list = oOrderDao.queryAllOutOrder(oOrder);
        LayTab layTab = new LayTab();
        if (list !=null){
            layTab.setCount(oOrderDao.queryAllOutOrderCount(oOrder));
            layTab.setData(list);
        }
        return layTab;
    }

    /**
     * 创建销售单
     * @param books
     * @param request
     * @return
     */
    @RequestMapping("/sale")
    public Result querById(@RequestBody ArrayList<Book> books, HttpServletRequest request) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        String o_orderindate =formatter1.format(date);
        Result result = new Result();
        OOrder oOrder = new OOrder();
        OOrderWj oOrderWj = new OOrderWj();
        if (books.size() > 0) {
            for (Book book : books) {
                int bookid = book.getBookid();
                int count = book.getPlancount();
                oOrderWj.setBookid(bookid);
                oOrderWj.setOcount(count);
                oOrderWj.setO_orderid(dateString);
                double v = oOrderDao.addOOrderWj(oOrderWj);
            }
            oOrder.setO_orderid(dateString);
            oOrder.setO_orderindate(o_orderindate);
            User user = (User) request.getSession().getAttribute("user");
            String  username = user.getUsername();
            oOrder.setO_name(username);
            int i = oOrderDao.addOOrder(oOrder);
            if (i > 0) {
                result.setCode(1);
                result.setMsg("发布成功");
            } else {
                result.setCode(0);
                result.setMsg("发布失败");
            }
        }
        return result;
    }

    /**
     * 查看销售订单详情表
     * @param
     * @return
     */
    @RequestMapping("/queryById")
    public LayTab queryById(OOrderWj oOrderWj){
        List<Map> list = oOrderDao.getOOrderWJ(oOrderWj);
        LayTab layTab = new LayTab();
        if (list !=null){
            layTab = new LayTab(list.size(),list);
        }
        return layTab;
    }


    @RequestMapping("/queryAllStatus1")
    public LayTab queryAllStatus1(OOrder oOrder){
        List<Map> list = oOrderDao.queryAllStatus1(oOrder);
        LayTab layTab = new LayTab();
        if (list!=null){
            layTab.setCount(oOrderDao.queryAllStatus1Count(oOrder));
            layTab.setData(list);
        }

        return layTab;
    }


    /**
     * 修改驳回销售订单的书籍数据
     *
     * @param OOrderWjs
     * @return
     */
    @RequestMapping("/updateReject")
    public Result updateReject(@RequestBody ArrayList<OOrderWj> OOrderWjs) {
        Result result = new Result();
        int i=0;
        if (OOrderWjs.size() > 0) {
            for (OOrderWj oOrderWj : OOrderWjs) {

                i = oOrderDao.updateReject(oOrderWj);

            }
            if (i > 0) {
                result.setCode(1);
                result.setMsg("修改成功");
            } else {
                result.setCode(0);
                result.setMsg("修改失败");
            }
        }
        return result;
    }

    @RequestMapping("/setStatus0")
    public Result setStatus0(OOrder oOrder){

        int i = oOrderDao.updateStatus0(oOrder);
        Result result = new Result();
        if (i > 0) {
            result.setCode(1);
            result.setMsg("提交成功");
        } else {
            result.setCode(0);
            result.setMsg("提交失败");
        }
        return result;
    }


}
